package com.yyfax.pay.quartz.adapter.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yyfax.framework.common.Log;
import com.yyfax.pay.common.annotation.DepositQueryTaskType;
import com.yyfax.pay.common.enums.FuiouBusiTypeEnum;
import com.yyfax.pay.depository.domain.FuiouQueryTask;
import com.yyfax.pay.depository.service.inner.ChargeInnerService;
import com.yyfax.pay.depository.vo.FuiouChargeVO;
import com.yyfax.pay.quartz.adapter.DepositQueryHandlerAdapter;
import com.yyfax.pay.quartz.model.vo.TaskResultVO;

/**
 * 查询充值结果
 * 
 * @author buyi
 * @date 2017-09-25 10:13:34
 * @since v2.0.0
 */
@Service
@DepositQueryTaskType(FuiouBusiTypeEnum.CHARGE_QUERY)
public class FuiouDepositChargeQueryHandler implements DepositQueryHandlerAdapter {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private ChargeInnerService chargeInnerService;

	@Override
	public TaskResultVO handle(FuiouQueryTask task) throws Throwable {
		final String op = "FuiouDepositChargeQueryHandler.handle";

		FuiouChargeVO vo = chargeInnerService.queryResult(task.getBusiId());
		if (vo.isResult()) {
			return new TaskResultVO(true);
		}

		// 如果达到最大查询次数，订单还是未出结果，则打印异常日志
		if (task.getOptNo() >= task.getMaxNo()) {
			logger.error(Log.op(op).msg("充值查询达到最大次数，订单还未出结果，需要人工介入。").kv("busiId", vo.getPo().getBusiId()).toString());
		}

		return new TaskResultVO(false);
	}

}
